<template>
  <div>
    <el-card style="margin:30px">
      <div slot="header">
        <el-button class="clearfix" type="primary" size="small" @click="addAddress">新增</el-button>
      </div>
      <el-table :data="addressList" border stripe>
        <el-table-column
          type="index"
          label="#"
          :width="70"
          fixed
          align="center"
        />
        <el-table-column
          prop="addressName"
          label="地址名称"
          :width="180"
          align="center"
        />
        <el-table-column
          label="默认发货地址"
          align="center"
          width="200"
        >
          <template slot-scope="scope">
            <span>默认发货地址:</span>
            <el-switch
              v-model="scope.row.sendStatus"
              :active-value="1"
              :inactive-value="0"
              @change="changeDeliver(scope.row.id)"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="name"
          label="收货人姓名"
          :width="180"
          align="center"
        />
        <el-table-column
          prop="phone"
          label="收货人电话"
          :width="180"
          align="center"
        />
        <el-table-column
          label="收货人地址"
          :width="220"
          align="center"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.province }}-</span>
            <span>{{ scope.row.city }}-</span>
            <span>{{ scope.row.region }}</span>
            <p>{{ scope.row.detailAddress }}</p>
          </template>
        </el-table-column>
        <el-table-column
          label="默认收货地址"
          align="center"
          width="200"
        >
          <template slot-scope="scope">
            <span>默认收货地址:</span>
            <el-switch
              v-model="scope.row.receiveStatus"
              :active-value="1"
              :inactive-value="0"
              @change="changeReceiving(scope.row.id)"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间"
          :width="200"
          align="center"
        />
        <el-table-column
          fixed="right"
          label="操作"
          align="center"
          width="150"
        >
          <template slot-scope="scope">
            <button style="color:red" @click="doDel(scope.row.id)">删除</button>
            <button style="color:#409eff" @click="goEdit(scope.row.id)">编辑</button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <newAddress ref="son" />
  </div>
</template>

<script>
import newAddress from '../newAddress'
import { companyAddress, addressList,
  setSendOne, setReceiveOne, companyAddres as editAddress } from '@/api/order/address'
export default {
  components: {
    newAddress
  },
  data() {
    return {
      addressList: []
    }
  },
  computed: {

  },
  created() {
    this.init()
  },
  mounted() {

  },
  methods: {
    init() {
      addressList().then(res => {
        const { success, data, message: errMge } = res
        if (success) {
          const { items } = data
          this.addressList = items
          console.log(this.addressList)
        } else {
          this.$message.error(errMge)
        }
      })
    },
    addAddress() { // 新增地址
      this.$refs.son.addressList = {}
      this.$refs.son.open()
    },
    changeReceiving(id) { // 设置默认收货
      setReceiveOne({
        id: id
      }).then(res => {
        const { success, message: errMge } = res
        if (success) {
          this.$message.success('设置默认收货成功')
          this.init()
        } else {
          this.$message.error(errMge)
        }
      })
    },
    changeDeliver(id) { // 设置默认发货
      setSendOne({
        id: id
      }).then(res => {
        const { success, message: errMge } = res
        if (success) {
          this.$message.success('设置默认发货成功')
          this.init()
        } else {
          this.$message.error(errMge)
        }
      })
    },
    doDel(id) { // 删除
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        companyAddress(id).then(res => {
          this.init()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      }).catch(() => {
      })
    },
    goEdit(id) { // 编辑
      editAddress(id).then(res => {
        const { success, data, message: errMge } = res
        if (success) {
          this.$refs.son.addressList = data.address
          console.log(this.$refs.son.addressList)
          this.$refs.son.open()
        } else {
          this.$message.error(errMge)
        }
        console.log(res)
      })
    }
  }
}
</script>

<style scoped lang="scss">
  button{
    border:none;
    margin: 0 3px;
    background-color: #fff;
  }
  .clearfix{
    background-color: #409eff;
  }
</style>
